# 设置 CMake 最低版本要求
cmake_minimum_required(VERSION 3.0)

# 定义项目名称
project(m400)

# 生成当前日期和时间
string(TIMESTAMP BUILD_DATETIME "%Y%m%d%H%M%S" UTC)
# 将日期和时间作为宏定义传递给程序
add_definitions(-DBUILD_DATETIME="${BUILD_DATETIME}")

# 设置 C/C++ 编译器
set(CMAKE_CXX_COMPILER "/opt/linux/x86-arm/aarch64-v01c01-linux-gnu-gcc/bin/aarch64-linux-gnu-g++")
set(CMAKE_C_COMPILER "/opt/linux/x86-arm/aarch64-v01c01-linux-gnu-gcc/bin/aarch64-linux-gnu-gcc")

# 设置 C++ 标准和编译选项
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
set(CMAKE_CXX_FLAGS "-std=c++17 -Wno-pedantic")

# 设置全局属性，启用编译命令导出
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# 根据构建类型设置额外的编译选项
if(CMAKE_BUILD_TYPE MATCHES Debug)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
elseif(CMAKE_BUILD_TYPE MATCHES Release)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
endif()

# 设置链接器选项
set(CMAKE_EXE_LINKER_FLAGS "-lm -lrt -ldl -pthread -latomic")

# 收集所有源文件目录
set(SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
set(INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
set(LIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lib)

# 定义公共头文件目录
include_directories(${SOURCE_DIR} ${INCLUDE_DIR})

# 定义公共库和链接目录
set(COMMON_LIBS hi_mpi securec hi_mpi_sysmem hi_mpi_ive hi_ivs_md svp_acl hi_mpi_km protobuf-c opencv_world shttpd crypto curl ssl z)
link_directories(${LIB_DIR})

# 定义不同目标的源文件
set(CLI_SRC
    ${SOURCE_DIR}/client.cpp
    ${SOURCE_DIR}/detr.cpp
    ${SOURCE_DIR}/npu.cpp
    ${SOURCE_DIR}/rgn.cpp
    ${SOURCE_DIR}/global.cpp
)

set(SRV_SRC
    ${SOURCE_DIR}/server.cpp
    ${SOURCE_DIR}/gimbal_client.cpp
    ${SOURCE_DIR}/detr.cpp
    ${SOURCE_DIR}/npu.cpp
    ${SOURCE_DIR}/global.cpp
    ${SOURCE_DIR}/rgn.cpp
    ${SOURCE_DIR}/serialPort.cpp
)

set(PID_SRC
    ${SOURCE_DIR}/pid.cpp
    ${SOURCE_DIR}/global.cpp
    ${SOURCE_DIR}/serialPort.cpp
)

# 移除重复的头文件，头文件不需要添加到 add_executable 中
# 头文件的作用是在编译源文件时被包含，而不是直接参与可执行文件的生成

# 添加可执行文件目标
add_executable(pid ${PID_SRC})
add_executable(srv ${SRV_SRC})
add_executable(cli ${CLI_SRC})
# add_executable(server_send_file ${SOURCE_DIR}/server_send_file.cpp)

# 添加共享库目标
# add_library(usrplatform SHARED ${SOURCE_DIR}/hcm_usrplatform_lib.c)

# 链接公共库到各个目标
foreach(TARGET pid srv cli)
    target_link_libraries(${TARGET} ${COMMON_LIBS})
endforeach()
    